﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hidden_Markov_Model
{
	class Program
	{
		static void Main(string[] args)
		{

			int[] sequence = null;
			//If there are no arguments then auto-generate a series of unfortunate events, Mr. Snicket
			if (args.Length == 0)
			{
				//generate a few random states and see what happens
				sequence = new int[9];
				Random rng = new Random();

				for (int i = 0; i < sequence.Length; i++)
					sequence[i] = rng.Next(3);
			}
			//Take in a string of event letters
			else if (args.Length == 1)
				sequence = StateElement.StringToEvents(args[0]);
			//Anything else and shut her down
			else
			{
				Console.WriteLine("Only one argument in the form of a string of characters representing the events to process");
				Console.WriteLine("Example: SZWWSZWZSWSZ");
				Environment.Exit(0);
			}
			

			List<StateElement.State> final = StateElement.Viterbi(sequence);

			Console.ResetColor();
			Console.WriteLine("Highest probable sequence:\n");

			Console.ForegroundColor = ConsoleColor.Gray;
			Console.WriteLine("C = Cloudy");
			Console.ForegroundColor = ConsoleColor.Blue;
			Console.WriteLine("R = Rainy");
			Console.ForegroundColor = ConsoleColor.Yellow;
			Console.WriteLine("S = Sunny\n");

			int index = -1;
			foreach(StateElement.State se in final)
			{
				if(se == StateElement.State.Begin)
				{
					Console.ForegroundColor = ConsoleColor.Green;
					Console.Write("Begin()->");
				}
				else if (se == StateElement.State.Cloudy)
				{
					Console.ForegroundColor = ConsoleColor.Gray;
					Console.Write("C({0})->", (StateElement.Emission)sequence[index]);
				}
				else if (se == StateElement.State.Raining)
				{
					Console.ForegroundColor = ConsoleColor.Blue;
					Console.Write("R({0})->", (StateElement.Emission)sequence[index]);
				}
				else
				{
					Console.ForegroundColor = ConsoleColor.Yellow;
					Console.Write("S({0})->", (StateElement.Emission)sequence[index]);
				}
				index++;
			}

			Console.ForegroundColor = ConsoleColor.Red;
			Console.Write("End()");

			Console.Read();

		}
	}
}
